<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="../connections/connectVB.asp"-->
<!-- #include file="Utility/freeASPUploadLibrary.asp" -->
<%
'Get ProductById
Dim ProdName, ProdPrice,Size, Description, ProdId,CatId, Images
ProdId = Request.QueryString("ProdId")
cID = Request.QueryString("CatID")
Function GetProdById

Set Recordset = Server.CreateObject("ADODB.Recordset")
Dim sql 
sql = "Select a.*, b.CategoryID,b.CategoryName from Product a join Category b on a.CategoryID = b.CategoryID where ProductID =" + ProdId
Recordset.Open sql,conn

ProdName = Recordset("ProductName")
ProdPrice = Recordset("Price")
Size = Recordset("Size")
Description = Recordset("Description")
CatId = Recordset("CategoryID")
Images = Recordset("Image")
CatName = Recordset("CategoryName")


'Close the connection and recordset objects to free up resources
Recordset.Close
Set Recordset=nothing
if conn.State =  adStateOpen then
        conn.Close
 End If
End Function
%>

<% 
 'Declare upload folder
 Dim uploadsDirVar
 uploadsDirVar = Server.MapPath("../Upload/ImageProd")

 
'Insert Image
function SaveFiles
    Dim Upload, fileName, fileSize, ks, i, fileKey, ImgName
	Dim hdProdImage
	
	Set Upload = New FreeASPUpload
    Upload.Save(uploadsDirVar)	
	hdProdImage =  Upload.Form("hdProdImage")

if (hdProdImage <> "" or hdProdImage <> null) then	
	' If something fails inside the script, but the exception is handled
	If Err.Number<>0 then Exit function

    SaveFiles = ""
    ks = Upload.UploadedFiles.keys

    if (UBound(ks) <> -1) then
        'SaveFiles = "<B>Files uploaded:</B> "		
        for each fileKey in Upload.UploadedFiles.keys
			'Upload.UploadedFiles(fileKey).FileName = "Nick.jpg"
            'SaveFiles = SaveFiles & Upload.UploadedFiles(fileKey).FileName & " (" & Upload.UploadedFiles(fileKey).Length & "B) "			
			ImgName = Upload.UploadedFiles(fileKey).FileName	
        next		
    end if
end if

'Update Product 
	Dim pName
	Dim CatId
	Dim pPrice	
	Dim ImgUrl
	Dim pSize
	Dim pDes
	
	pName = Upload.Form("pName")
	CatId = Upload.Form("CatID")
	pPrice = Upload.Form("pPrice")
	ImgUrl = Upload.Form("ProdImg")
	pSize = Upload.Form("pSize")
	pDes = Upload.Form("pDes")
	pID = Upload.Form("ProdID")
	
	'if Discount = "" then
		'Discount = ""
	'End if
	
	
	'Don't update image if not specify new image.	
	if (hdProdImage = "" or hdProdImage = null) then
		sql = "UPDATE Product SET ProductName='"+pName+"',[CategoryID]="+CatId+",[Price]="+pPrice+",[Size]="+pSize+",[Description]='"+pDes+"' WHERE ProductID="+pID
		
		else 'Update image if new image is spcified
	sql = "UPDATE Product SET ProductName='"+pName+"',[CategoryID]="+CatId+",[Price]="+pPrice+",[Image]='"+ImgName+"',[Size]="+pSize+",[Description]='"+pDes+"' WHERE ProductID="+pID
	End if
	
	conn.Execute(sql)
	
	if conn.State =  adStateOpen then
        conn.Close
    End If	
	
Response.Redirect("product.asp")
end function
%>

  <%  
  	if Request.ServerVariables("REQUEST_METHOD") = "POST" then  		 
		 response.write SaveFiles()
	elseif Request.ServerVariables("REQUEST_METHOD") = "GET" then
		response.write GetProdById		 
	end if
	
  %>
 <!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0" />
	<title>Dashboard | Shoes </title>

	<!--=== CSS ===-->

	<!-- Bootstrap -->
	<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />

	<!-- jQuery UI -->
	<!--<link href="plugins/jquery-ui/jquery-ui-1.10.2.custom.css" rel="stylesheet" type="text/css" />-->
	<!--[if lt IE 9]>
		<link rel="stylesheet" type="text/css" href="plugins/jquery-ui/jquery.ui.1.10.2.ie.css"/>
	<![endif]-->

	<!-- Theme -->
	<link href="assets/css/main.css" rel="stylesheet" type="text/css" />
	<link href="assets/css/plugins.css" rel="stylesheet" type="text/css" />
	<link href="assets/css/responsive.css" rel="stylesheet" type="text/css" />
	<link href="assets/css/icons.css" rel="stylesheet" type="text/css" />

	<link rel="stylesheet" href="assets/css/fontawesome/font-awesome.min.css">
	<!--[if IE 7]>
		<link rel="stylesheet" href="assets/css/fontawesome/font-awesome-ie7.min.css">
	<![endif]-->

	<!--[if IE 8]>
		<link href="assets/css/ie8.css" rel="stylesheet" type="text/css" />
	<![endif]-->
	<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,600,700' rel='stylesheet' type='text/css'>

	<!--=== JavaScript ===-->

	<script type="text/javascript" src="assets/js/libs/jquery-1.10.2.min.js"></script>
	<script type="text/javascript" src="plugins/jquery-ui/jquery-ui-1.10.2.custom.min.js"></script>

	<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
	<script type="text/javascript" src="assets/js/libs/lodash.compat.min.js"></script>

	<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
	<!--[if lt IE 9]>
		<script src="assets/js/libs/html5shiv.js"></script>
	<![endif]-->

	<!-- Smartphone Touch Events -->
	<script type="text/javascript" src="plugins/touchpunch/jquery.ui.touch-punch.min.js"></script>
	<script type="text/javascript" src="plugins/event.swipe/jquery.event.move.js"></script>
	<script type="text/javascript" src="plugins/event.swipe/jquery.event.swipe.js"></script>

	<!-- General -->
	<script type="text/javascript" src="assets/js/libs/breakpoints.js"></script>
	<script type="text/javascript" src="plugins/respond/respond.min.js"></script> <!-- Polyfill for min/max-width CSS3 Media Queries (only for IE8) -->
	<script type="text/javascript" src="plugins/cookie/jquery.cookie.min.js"></script>
	<script type="text/javascript" src="plugins/slimscroll/jquery.slimscroll.min.js"></script>
	<script type="text/javascript" src="plugins/slimscroll/jquery.slimscroll.horizontal.min.js"></script>

	<!-- Page specific plugins -->
	<!-- Charts -->
	<script type="text/javascript" src="plugins/sparkline/jquery.sparkline.min.js"></script>

	<script type="text/javascript" src="plugins/daterangepicker/moment.min.js"></script>
	<script type="text/javascript" src="plugins/daterangepicker/daterangepicker.js"></script>
	<script type="text/javascript" src="plugins/blockui/jquery.blockUI.min.js"></script>

	<!-- Forms -->
	<script type="text/javascript" src="plugins/uniform/jquery.uniform.min.js"></script> <!-- Styled radio and checkboxes -->
	<script type="text/javascript" src="plugins/select2/select2.min.js"></script> <!-- Styled select boxes -->
	<script type="text/javascript" src="plugins/fileinput/fileinput.js"></script>

	<!-- Form Validation -->
	<script type="text/javascript" src="plugins/validation/jquery.validate.min.js"></script>
	<script type="text/javascript" src="plugins/validation/additional-methods.min.js"></script>

	<!-- Noty -->
	<script type="text/javascript" src="plugins/noty/jquery.noty.js"></script>
	<script type="text/javascript" src="plugins/noty/layouts/top.js"></script>
	<script type="text/javascript" src="plugins/noty/themes/default.js"></script>

	<!-- App -->
	<script type="text/javascript" src="assets/js/app.js"></script>
	<script type="text/javascript" src="assets/js/plugins.js"></script>
	<script type="text/javascript" src="assets/js/plugins.form-components.js"></script>


	<script>
	$(document).ready(function(){
		"use strict";

		App.init(); // Init layout and core plugins
		Plugins.init(); // Init all plugins
		FormComponents.init(); // Init all form-specific plugins
	});
	</script>

	<!-- Demo JS -->
	<script type="text/javascript" src="assets/js/custom.js"></script>
	<script type="text/javascript" src="assets/js/demo/form_validation.js"></script>
</head>

<body>

	<!-- Header -->
	<header class="header navbar navbar-fixed-top" role="banner">
		<!-- Top Navigation Bar -->
		<div class="container">

			<!-- Only visible on smartphones, menu toggle -->
			<ul class="nav navbar-nav">
				<li class="nav-toggle"><a href="javascript:void(0);" title=""><i class="icon-reorder"></i></a></li>
			</ul>

			<!-- Logo -->
			<a class="navbar-brand" href="index.asp">
				<strong>SH</strong>OES
			</a>
			<!-- /logo -->

			<!-- Sidebar Toggler -->
			<a href="#" class="toggle-sidebar bs-tooltip" data-placement="bottom" data-original-title="Toggle navigation">
				<i class="icon-reorder"></i>
			</a>
			<!-- /Sidebar Toggler -->

			<!-- Top Left Menu -->
			<ul class="nav navbar-nav navbar-left hidden-xs hidden-sm">
				<li>
					<a href="index.asp">
						Dashboard
					</a>
				</li>
			</ul>
			<!-- /Top Left Menu -->

			<!-- Top Right Menu -->
			<ul class="nav navbar-nav navbar-right">
				<!-- User Login Dropdown -->
				<li class="dropdown user">
					<a href="#" class="dropdown-toggle" data-toggle="dropdown">
						<i class="icon-male"></i>
						<span class="username"><%Response.Write(admin)%></span>
						<i class="icon-caret-down small"></i>
					</a>
					<ul class="dropdown-menu">
						<li><a href="logout.asp"><i class="icon-key"></i> Log Out</a></li>
					</ul>
				</li>
				<!-- /user login dropdown -->
			</ul>
			<!-- /Top Right Menu -->
		</div>
		<!-- /top navigation bar -->
	</header> <!-- /.header -->

	<div id="container">
		<div id="sidebar" class="sidebar-fixed">
			<div id="sidebar-content">

				<!--=== Navigation ===-->
				<ul id="nav">
					<li class="current">
						<a href="index.asp">
							<i class="icon-home"></i>
							Home
						</a>
					</li>
					<li>
						<a href="javascript:void(0);">
							<i class="icon-barcode"></i>
							Product
						</a>
						<ul class="sub-menu">
							<li>
								<a href="category.asp">
								<i class="icon-angle-right"></i>
								Category
								</a>
							</li>
							<li>
								<a href="product.asp">
								<i class="icon-angle-right"></i>
								Manage
								</a>
							</li>
						</ul>
					</li>
					<li>
						<a href="javascript:void(0);">
							<i class="icon-user"></i>
							User
						</a>
						<ul class="sub-menu">
							<li>
								<a href="admin.asp">
								<i class="icon-angle-right"></i>
								Admin
								</a>
							</li>
							<li>
								<a href="customer.asp">
								<i class="icon-angle-right"></i>
								Customer
								</a>
							</li>
						</ul>
					</li>
					<li >
						<a href="order.asp">
							<i class="icon-shopping-cart"></i>
							Order
						</a>
					</li>
                     <li class="current">
						<a href="question.asp">
							<i class="icon-comments"></i>
							Security Management
						</a>
					</li>
					<li >
						<a href="feedback.asp">
							<i class="icon-comments"></i>
							Feedback
						</a>
					</li>                   
					<li >
						<a href="report.asp">
							<i class="icon-print"></i>
							Report
						</a>
					</li>

				</ul>
				<!-- /Navigation -->
			</div>
			<div id="divider" class="resizeable"></div>
		</div>
		<!-- /Sidebar -->

		<div id="content">
			<div class="container">
				<!-- Breadcrumbs line -->
				<div class="crumbs">
					<ul id="breadcrumbs" class="breadcrumb">
						<li>
							<i class="icon-home"></i>
							<a href="index.asp">Home</a>
						</li>
						<li class="current">
							<a href="product.asp" title="">Product</a>
						</li>
					</ul>

					<ul class="crumb-buttons">
						<li><a href="charts.asp" title=""><i class="icon-signal"></i><span>Statistics &amp Report</span></a></li>
					</ul>
				</div>
				<!-- /Breadcrumbs line -->

				<!--=== Page Header ===-->
				<div class="page-header">
					<div class="page-title">
						<h3>Product</h3>
						<span>Edit</span>
					</div>
				</div>
				<!-- /Page Header -->
			
				<!--=== Page Content ===-->
				<div class="row">
					<div class="col-md-12">
						<div class="widget box">
							<div class="widget-header">
								<h4><i class="icon-edit"></i> Edit Product</h4>
							</div>
							<div class="widget-content">
								<form class="form-horizontal row-border" enctype="multipart/form-data" id="validate-1" action="product_update.asp" method="post" onsubmit="return onSubmitForm();">
									<div class="form-group">
										<label class="col-md-3 control-label">Product Name<span class="required">*</span></label>
										<div class="col-md-9">
											<input value="<% Response.Write(ProdName)%>" type="text" name="pName" class="form-control required">
                                            <input type="hidden" id="ProdId" name="ProdId" value="<% Response.Write(ProdId)%>"/>
										</div>
									</div>
									<div class="form-group">
										<label class="col-md-3 control-label">CategoryName</label>                                                                                
										<div class="col-md-9">
											<select name="catID" class="form-control required">
                                        <%
											sql ="select * from Category"
											Set rs=conn.Execute(sql)																																			
											 do  while not rs.EOF %>                                             
											<option value="<%=rs("CategoryID")%>"><%=rs("CategoryName")%></option>
									<%
										rs.MoveNext	
										loop							
									%>																							
											</select>
										</div>
									</div>
									<div class="form-group">
										<label class="col-md-3 control-label">Price<span class="required">*</span></label>
										<div class="col-md-9">
											<div>
												<input type="text" name="pPrice" value="<%Response.Write(ProdPrice)%>" class="form-control spinner required" min="20" max="100000">
											</div>
											<p class="help-block">Must be greater than 20 $ or less than 200,000 $.</p>
											<label for="spinner-validation" generated="true" class="has-error help-block" style="display:none;"></label>
										</div>
									</div>
									<div class="form-group">
										<label class="col-md-3 control-label">Image<span class="required">*</span></label>
										<div class="col-md-9">
											<input name="ProdImg" id="ProdImg" class="form-control" type="file" >   
		         							<input type="hidden" name="hdProdImage" id="hdProdImage" value="" />
											<p class="help-block">Images only (image/*)</p>
                                         <div class="col-lg-10">
        						<img style="width:50%" src="../Upload/ImageProd/<%Response.Write(Images)%>"/> 
        </div>
											<label for="file1" class="has-error help-block" generated="true" style="display:none;"></label>
										</div>
									</div>
									<div class="form-group">
										<label class="col-md-3 control-label">Size<span class="required">*</span></label>
										<div class="col-md-9">
											<div>
												<input type="text" name="pSize" value="<%Response.Write(Size)%>" class="form-control spinner required" min="27" max="45" value="<%Response.Write(Size)%>">
											</div>
											<p class="help-block">Must be greater than 27 or less than 45.</p>
											<label for="spinner-validation" generated="true" class="has-error help-block" style="display:none;"></label>
										</div>
									</div>
									<div class="form-group">
										<label class="col-md-3 control-label">Description</label>
										<div class="col-md-9"><textarea rows="5" name="pDes" class="form-control wysiwyg"><%Response.Write(Description)%></textarea></div>
									</div>
									<div class="form-actions">
										<input type="submit" value="Submit" class="btn btn-primary pull-right">
                                        <a class="btn btn-primary pull-right" href="product.asp" > Cancel</a>
									</div>

								</form>
							</div>
						</div>
						<!-- /Validation Example 1 -->
					</div>
				<!--=== Managed Tables ===-->

				</div> <!-- /.row -->
				<!-- /Page Content -->

			</div>
			<!-- /.container -->

		</div>
	</div>

</body>
</html>


<script type="text/javascript">
	//Change Product Image Name to Unique
function onSubmitForm()
	{
		var nameString = jQuery("#ProdImg").val();
		var nameArray = nameString.split('\\');
		var name = nameArray[nameArray.length - 1];
		if(name != "")
		{
			name = name.substr(name.lastIndexOf("."));
			name = Date.parse(new Date())+name;
			jQuery("#hdProdImage").val(name) ;
			//jQuery("#ProdImg").val(name) ;	
			//alert($("#hdProdImage").val());	
		}
		else
			jQuery("#hdProdImage").val("") ;	
		return true;	
	}
	
	function showimagepreview(input) {
		if (input.files && input.files[0]) {
		var filerdr = new FileReader();
		filerdr.onload = function(e) {
		$('#imgprvw').attr('src', e.target.result);
			}
		filerdr.readAsDataURL(input.files[0]);
		}
	}

</script>